package com.wolfsea.viewpagerrecyclerviewpageing.pagemenulibrary;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import com.wolfsea.viewpagerrecyclerviewpageing.R;
import com.wolfsea.viewpagerrecyclerviewpageing.pagemenulibrary.adapter.EntranceAdapter;
import com.wolfsea.viewpagerrecyclerviewpageing.pagemenulibrary.adapter.PageViewPagerAdapter;
import com.wolfsea.viewpagerrecyclerviewpageing.pagemenulibrary.holder.PageMenuViewHolderCreator;
import com.wolfsea.viewpagerrecyclerviewpageing.pagemenulibrary.itemdecoration.GridLayoutDecoration;
import com.wolfsea.viewpagerrecyclerviewpageing.pagemenulibrary.itemdecoration.GridLayoutDecorationLine;
import com.wolfsea.viewpagerrecyclerviewpageing.utils.ScreenUtil;
import java.util.ArrayList;
import java.util.List;

public class PageMenuLayout extends RelativeLayout {

    private static final int DEFAULT_ROW_COUNT = 2;
    private static final int DEFAULT_SPAN_COUNT = 5;
    private CustomViewPager mViewPager;

    /**
     * 行数
     */
    private int mRowCount = DEFAULT_ROW_COUNT;

    /**
     * 列数
     */
    private int mSpanCount = DEFAULT_SPAN_COUNT;

    public PageMenuLayout(Context context) {
        this(context, null, 0);
    }

    public PageMenuLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PageMenuLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        mViewPager = new CustomViewPager(context);
        mViewPager.setOverScrollMode(ViewPager.OVER_SCROLL_NEVER);
        addView(mViewPager, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PageMenuLayout);
        if (typedArray != null) {
            mRowCount = typedArray.getInteger(R.styleable.PageMenuLayout_pagemenu_row_count, DEFAULT_ROW_COUNT);
            mSpanCount = typedArray.getInteger(R.styleable.PageMenuLayout_pagemenu_span_count, DEFAULT_SPAN_COUNT);
            typedArray.recycle();
        }
    }

    public void setPageDataSet(@NonNull List<?> dataSet, @NonNull PageMenuViewHolderCreator creator) {
        setPageDataSet(mRowCount, mSpanCount, dataSet, creator);
    }

    /**
     * @return 菜单总页数
     */
    public int getPageCount() {
        if (mViewPager != null && mViewPager.getAdapter() != null) {
            return mViewPager.getAdapter().getCount();
        } else {
            return 0;
        }
    }

    /**
     * 页面滚动监听
     *
     * @param pageListener
     */
    public void setOnPageListener(ViewPager.OnPageChangeListener pageListener) {
        if (mViewPager != null) {
            mViewPager.addOnPageChangeListener(pageListener);
        }
    }

    /**
     * 设置行数
     *
     * @param rowCount
     */
    public void setRowCount(int rowCount) {
        mRowCount = rowCount;
    }

    /**
     * 设置列数
     *
     * @param spanCount
     */
    public void setSpanCount(int spanCount) {
        mSpanCount = spanCount;
    }

    public void setPageDataSet(int rowCount, int spanCount, @NonNull List<?> dataSet, @NonNull PageMenuViewHolderCreator creator) {
        mRowCount = rowCount;
        mSpanCount = spanCount;
        if (mRowCount == 0 || mSpanCount == 0) {
            return;
        }
        int pageSize = mRowCount * mSpanCount;
        int pageCount = (int) Math.ceil(dataSet.size() * 1.0 / pageSize);
        List<View> viewList = new ArrayList<>();
        for (int index = 0; index < pageCount; index++) {

            View root = LayoutInflater.from(this.getContext()).inflate(R.layout.item_rv_layout, null);
            RecyclerView recyclerView = root.findViewById(R.id.item_rv);

            int space = ScreenUtil.dip2px(this.getContext().getResources().getDimension(R.dimen.dp_5));
            //GridLayoutDecoration decoration = new GridLayoutDecoration(space, spanCount);
            recyclerView.addItemDecoration(new GridLayoutDecorationLine(this.getContext(), rowCount, spanCount, GridLayoutDecorationLine.TYPE_TWO));

            //int width = ViewGroup.LayoutParams.MATCH_PARENT;
            //int height = ViewGroup.LayoutParams.MATCH_PARENT;
            //RelativeLayout.LayoutParams layoutParams = new LayoutParams(width, height);
            //recyclerView.setLayoutParams(layoutParams);

            int gWidth = ScreenUtil.dip2px(R.dimen.dp_80);
            int gHeight = ScreenUtil.dip2px(R.dimen.dp_300);
            DefineGridLayoutManager layoutManager = new DefineGridLayoutManager(this.getContext(), mSpanCount, gWidth, gHeight);
            recyclerView.setLayoutManager(layoutManager);

            EntranceAdapter<?> entranceAdapter = new EntranceAdapter<>(creator, dataSet, index, pageSize);
            recyclerView.setAdapter(entranceAdapter);

            viewList.add(root);
        }

        PageViewPagerAdapter adapter = new PageViewPagerAdapter(viewList);
        mViewPager.setAdapter(adapter);
    }

}
